﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
using PDAMain.Entity;
using System.Runtime.InteropServices;

namespace PDAMain.Forms.InspectionWork
{
    public partial class InspectionToFastRepair : BaseForm
    {
        public string assignmentId;
        public InspectionToFastRepair()
        {
            InitializeComponent();
           
        }


        private void InspectionToFastRepair_Load(object sender, EventArgs e)
        {

            ITFRBind();
            // 打开BarCode扫描设备
           /* if (!OpenBarCode())
            {
                int ErrCode = Marshal.GetLastWin32Error();
                MessageBox.Show("Fail to Open BarCode :(, ErrCode = " + ErrCode.ToString(), "Error");
            }

            EnableBeeper(true);
            EnableVibrater(true);*/

        }
        private void ITFRBind()
        {
            DataSet ds = DataHelper.InspectionWorkDao.getDetail(assignmentId);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                lblASSETNUM.Text = ds.Tables[0].Rows[0]["ASSETNUM"].ToString().Trim();
                lblASSDESCRIPTION.Text = ds.Tables[0].Rows[0]["ASSDESCRIPTION"].ToString().Trim();
                lblLOCATION.Text = ds.Tables[0].Rows[0]["LOCATION"].ToString().Trim();
                lblLOCDESCRIPTION.Text = ds.Tables[0].Rows[0]["LOCDESCRIPTION"].ToString().Trim();
                lblName.Text = Identity.dutyPerson;
                //txtRemark.Text = ds.Tables[0].Rows[0]["remark"].ToString().Trim();
            }
            else
            {
                MessageBox.Show("无数据!");
                this.Close();
            }
        }

        // 重载 基类BaseForm的这个方法
       /* public override void OnBarCodeNotify(byte[] BarCodeData, int nLength)
        {
            EnableBarCode(false);
            string BarCode = Encoding.Default.GetString(BarCodeData, 0, nLength);
            lblASSETNUM.Text = BarCode;

            scanFlag = false;
            EnableBarCode(true);
        }*/

        private void menuItem1_Click(object sender, EventArgs e)
        {
            if (txtRemark.Text.ToString().Trim() ==string.Empty)
            {
                MessageBox.Show("请填写备注！"); 
                return;
            }
            if (MessageBox.Show("你确定要执行此操作吗？", "提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                Identity.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                //if (DataHelper.InspectionWorkDao.deal(assignmentId, "9", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), txtRemark.Text.ToString()))
                if (DataHelper.InspectionWorkDao.FastWorkOrder(assignmentId, "9", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), txtRemark.Text.ToString(), lblLOCATION.Text, lblASSETNUM.Text,txtEstdur.Text))
                {
                    MessageBox.Show("成功同步数据至远程数据库");
                }
                else 
                {
                    bool Flag1 = DataHelper.InspectionWorkDao.insertLacolTable(assignmentId, "9", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), txtRemark.Text.ToString());
                    bool Flag2 = DataHelper.ApplyToRepairDao.insertLacolTable(lblASSETNUM.Text, Identity.dutyPerson, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), txtRemark.Text.ToString(), "", "0", lblLOCATION.Text,txtEstdur.Text.ToString());
                    if (Flag1 && Flag2)
                    {
                        MessageBox.Show("同步数据失败，保存至本地数据库");
                    }
                    else
                    {
                        MessageBox.Show("保存失败");
                    }
                }
                backToList();
            }
        }
        private void menuItem2_Click(object sender, EventArgs e)
        {
            // 关闭BarCode扫描设备
           // CloseBarCode();
            InspectionList list = new InspectionList();
            list.Show();
            this.Close();
            
        }

        private void InspectionToFastRepair_Closed(object sender, EventArgs e)
        {
            // 关闭BarCode扫描设备
           // CloseBarCode();
        }

        private void backToList()
        {
            //CloseBarCode();

            InspectionList list = new InspectionList();
            list.Show();
            this.Close();
        }
    }
}